import { Body, Controller, Post, Headers } from '@nestjs/common';
import { DiaryService } from './diary.service';
import { CreateDiaryDto, FindDiaryDto, UpdateDiaryDto, CreateDiaryBillDto, UpdateDiaryBillDto, BindDiaryDto } from './dto/diary.dto';
import { getResponse, ERROR_CODE, ERROR_MESSAGE } from 'src/common/const/const';
import { AuthService } from 'src/services/Auth';
import { VipUsageService } from 'src/vipUsage/vipUsage.service';
import { VipInfoService } from 'src/vipInfo/vipInfo.service';
import { addRagTask } from 'src/common/api/tools';
import { generateUniqueUserId } from 'src/common/utils/uuid';
import { NonVipDiaryBillLimit } from 'src/common/const/const';
import {serviceErrorLog} from "../common/out/logger/access.logger";

@Controller('diary')
export class DiaryController {
  constructor(
    private readonly diaryService: DiaryService,
    private readonly authService: AuthService,
    private readonly vipInfoService: VipInfoService,
    private readonly vipUsageService: VipUsageService
  ) { }

  // 创建日记本
  @Post('/createBill')
  async createBill(@Body() createDiaryDto: CreateDiaryBillDto, @Headers() headers: any) {
    // 生成日记唯一id
    const userInfo = this.authService.checkAuth(headers);
    if (!userInfo) {
      return getResponse(ERROR_CODE.VERIFY_EXPIRE, ERROR_MESSAGE[ERROR_CODE.VERIFY_EXPIRE], {})
    }

    const userid = userInfo.userid
    const isVip = await this.vipInfoService.isVipUser(userid)
    // 非 vip 需要检查，该功能是否已经超过限制
    if (!isVip) {
      const dairyBillCnt = await this.vipUsageService.getDiaryBillCnt(userid)
      if (dairyBillCnt > NonVipDiaryBillLimit) {
        return getResponse(ERROR_CODE.VIP_USAGE_COUNT_REACH_LIMIT, ERROR_MESSAGE[ERROR_CODE.VIP_USAGE_COUNT_REACH_LIMIT], {})
      }
    }

    const uniqueUserId = "diaryBill_" + generateUniqueUserId();
    let response = await this.diaryService.createBill({ userid: userid, diarybillid: uniqueUserId, ...createDiaryDto });
    // 更新 vip 功能使用次数
    await this.vipUsageService.addDiaryBillCnt(userid);

    return getResponse(0, "success", response);
  }


  @Post('/create')
  async createDiary(@Body() createDiaryDto: CreateDiaryDto, @Headers() headers: any) {
    const userInfo = this.authService.checkAuth(headers);
    if (!userInfo) {
      return getResponse(ERROR_CODE.VERIFY_EXPIRE, ERROR_MESSAGE[ERROR_CODE.VERIFY_EXPIRE], {})
    }
    const userid = userInfo.userid

    const uniqueUserId = "diary_" + generateUniqueUserId();
    let response = await this.diaryService.create({ userid: userid, diaryid: uniqueUserId, ...createDiaryDto });
    // 如果用户发布成功则写入知识库处理队列
    try {
      addRagTask('text_' + userInfo.userid, createDiaryDto.content);
    } catch (error) {
      serviceErrorLog('Error fetching data: '+ error);
    }
    // 更新 vip 功能使用次数
    await this.vipUsageService.addDiaryCnt(userid)
    return getResponse(0, "success", response);
  }

  @Post('/find')
  async findDiary(@Body() diaryFindDto: FindDiaryDto, @Headers() headers: any) {
    // 日记字符串id创建
    const userInfo = this.authService.checkAuth(headers);
    if (!userInfo) {
      return getResponse(ERROR_CODE.VERIFY_EXPIRE, ERROR_MESSAGE[ERROR_CODE.VERIFY_EXPIRE], {})
    }
    let response = await this.diaryService.find({ userid: userInfo.userid, ...diaryFindDto });
    return getResponse(0, "success", response);
  }


  @Post('/update')
  async updateDiary(@Body() updateDiaryDto: UpdateDiaryDto, @Headers() headers: any) {
    // 日记字符串id创建
    const userInfo = this.authService.checkAuth(headers);
    if (!userInfo) {
      return getResponse(ERROR_CODE.VERIFY_EXPIRE, ERROR_MESSAGE[ERROR_CODE.VERIFY_EXPIRE], {})
    }
    let response = await this.diaryService.update({ userid: userInfo.userid, ...updateDiaryDto });
    return getResponse(0, "success", response);
  }

  @Post('/updateDiaryBill')
  async updateDiaryBill(@Body() updateDiaryBillDto: UpdateDiaryBillDto, @Headers() headers: any) {
    // 日记字符串id创建
    const userInfo = this.authService.checkAuth(headers);
    if (!userInfo) {
      return getResponse(ERROR_CODE.VERIFY_EXPIRE, ERROR_MESSAGE[ERROR_CODE.VERIFY_EXPIRE], {})
    }
    let response = await this.diaryService.updateBill({ userid: userInfo.userid, ...updateDiaryBillDto });
    return getResponse(0, "success", response);
  }

  // 日记本绑定
  @Post('/bindDiaryBill')
  async bindDiaryBill(@Body() bindDiaryDto: BindDiaryDto, @Headers() headers: any) {
    const userInfo = this.authService.checkAuth(headers);
    if (!userInfo) {
      return getResponse(ERROR_CODE.VERIFY_EXPIRE, ERROR_MESSAGE[ERROR_CODE.VERIFY_EXPIRE], {})
    }
    // 初期 恋爱日记本绑定
    let response = await this.diaryService.bindBill({ userid: userInfo.userid, ...bindDiaryDto });
    return getResponse(0, "success", response);
  }
}
